package com.koron.housing.contract.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.contract.bean.dto.HousingContractDTO;
import com.koron.housing.contract.bean.param.HousingContractBillParam;
import com.koron.housing.contract.bean.po.HousingContract;
import com.koron.housing.contract.bean.query.ContractQuery;
import com.koron.housing.contract.bean.vo.HousingContractVO;
import com.koron.housing.contract.service.HousingContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * 合同信息(HousingContract)表控制层
 *
 * @author jzp
 * @since 2022-03-07 14:45:25
 */
@Api(tags = "合同信息接口")
@RestController
@RequestMapping("housingContract")
public class HousingContractController {
    /**
     * 服务对象
     */
    @Autowired
    private HousingContractService housingContractService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<HousingContractDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingContractService, "getDtoById", HousingContractDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<HousingContractVO>> page(PageQuery pageQuery, ContractQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingContractService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "地址、小区、财务报销编号、承租方集合查询", notes = "第一个为地址，第二个为小区，第三个为财务报销编号，第四个为承租方")
    @GetMapping("village")
    public Response<List<List<HousingContract>>> village(String propertySource) {
        return Response.ok(Arrays.asList(
                ADOConnection.runTask(getCurrDs(), housingContractService, "address", List.class),
                ADOConnection.runTask(getCurrDs(), housingContractService, "village", List.class),
                ADOConnection.runTask(getCurrDs(), housingContractService, "billCode", List.class),
                ADOConnection.runTask(getCurrDs(), housingContractService, "tenantry", List.class)
        ));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody HousingContract housingContract) {
        ADOConnection.runTask(getCurrDs(), housingContractService, "save", Boolean.class, housingContract);
        return Response.ok(housingContract.getId());
    }


    @ApiOperation(value = "续租")
    @PutMapping("renewal")
    public Response<String> renewal(@RequestBody HousingContract housingContract) {
        ADOConnection.runTask(getCurrDs(), housingContractService, "renewal", Boolean.class, housingContract);
        return Response.ok(housingContract.getId());
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public Response<String> update(@RequestBody HousingContract housingContract) {
        ADOConnection.runTask(getCurrDs(), housingContractService, "update", Boolean.class, housingContract);
        return Response.ok(housingContract.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), housingContractService, "deleteById", Boolean.class, id);
        return Response.ok();
    }

    @ApiOperation(value = "财务报销", notes = "返回billCode+年份+月份，如CW202201202201")
    @PostMapping("billSubmit")
    public Response<String> billSubmit(@RequestBody HousingContractBillParam housingContractBillParam) {
        ADOConnection.runTask(getCurrDs(), housingContractService, "billSubmit", Boolean.class, housingContractBillParam);
        return Response.ok(housingContractBillParam.getBillCode() + housingContractBillParam.getYear() + String.format("%02d", housingContractBillParam.getMonth()));
    }

    @SneakyThrows
    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, ContractQuery query) {
        List<HousingContractVO> list = ADOConnection.runTask(getCurrDs(), housingContractService, "list", List.class, query);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("合同信息", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), HousingContractVO.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("合同信息")
                .doWrite(list);
    }

}
